package org.yxing.palpalgps.domain;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import org.yxing.palpalgps.http.Download;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageSwitcher;
import android.widget.ImageView;

public class AsyncImageLoader {
    private static final String TAG = AsyncImageLoader.class.getSimpleName();
    
    //SoftReference 是软引用，为了系统更好的回收变量
    private HashMap<String, SoftReference<Drawable>> imageCache;
    
    public AsyncImageLoader() {
        imageCache = new HashMap<String, SoftReference<Drawable>>();
    }
    
    public Drawable loadDrawable(final String imageUrl, final ImageView imageView, final ImageCallback imageCallback) {
        
        if (imageCache.containsKey(imageUrl)) {
            //从缓存中读取
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);
            Drawable drawable = softReference.get();
            if (drawable != null) {
                imageCallback.imageLoaded(drawable, imageView, imageUrl);
                return drawable;
            }
        }
//        Log.v(TAG, "load image : " + imageUrl);
        
        final Handler mHandler = new Handler(){
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                
                Drawable drawable = (Drawable) msg.obj;
                imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
                if (imageCallback != null)
                    imageCallback.imageLoaded(drawable, imageView, imageUrl);
            }
        };
        
        //建立新的线程下载图片
        new Thread(){
            public void run() {
                Drawable drawable = Download.loadimgDrawable(imageUrl);
                mHandler.sendMessage( mHandler.obtainMessage(0, drawable));
            }
        }.start();
        
        return null;
    }
    
    /**
     * 不需要callback
     * @param imageUrl
     * @param imageView
     * @return
     */
    public Drawable loadDrawable(final String imageUrl, final ImageView imageView) {
        return loadDrawable(imageUrl, imageView, new ImageCallback() {
            
            public void imageLoaded(Drawable imageDrawable, ImageView imageView,
                    String imageUrl) {
                imageView.setImageDrawable(imageDrawable);
            }
        });
    }
    
    public Drawable loadDrawable(final String imageUrl, final ImageSwitcher imageSwitcher, final ImageSwitcherCallback imageCallback) {
        Log.v(TAG, "load drawable : " + imageUrl);
        if (imageCache.containsKey(imageUrl)) {
            //从缓存中读取
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);
            Drawable drawable = softReference.get();
            if (drawable != null) {
                imageCallback.imageLoaded(drawable, imageSwitcher, imageUrl);
                return drawable;
            }
        }
        
        final Handler mHandler = new Handler(){
            public void handleMessage(Message msg) {
                imageCallback.imageLoaded((Drawable) msg.obj, imageSwitcher, imageUrl);
                super.handleMessage(msg);
            }
        };
        
        //建立新的线程下载图片
        new Thread(){
            public void run() {
                Drawable drawable = Download.loadimgDrawable(imageUrl);
                imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
                Message msg = mHandler.obtainMessage(0, drawable);
                mHandler.sendMessage(msg);
            }
        }.start();
        
        return null;
    }
    
    //Call Back
    public interface ImageCallback {
        public void imageLoaded(Drawable imageDrawable, ImageView imageView, String imageUrl);
    }
    
    public interface ImageSwitcherCallback {
        public void imageLoaded(Drawable drawable, ImageSwitcher imageSwitcher, String imageUrl);
    }
}